//
//  UITabBarItem+DY.m
//  DYLYJ
//
//  Created by JM on 15/10/27.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "UIBarButtonItem+DY.h"
#import "DYNavigationController.h"
#import "SubViewOutOfSelfAlsoCanBeFirstresponseView.h"
@implementation UIBarButtonItem (DY)

//可以让button上下居中
+ (instancetype)_barItemWithCustomButton:(UIButton *)customButton {
    CGSize btnSize = customButton.frame.size;
    customButton.frame = CGRectMake(0, -(kDYBarDefaultHeight-44)/2.0, btnSize.width, btnSize.height);
    SubViewOutOfSelfAlsoCanBeFirstresponseView * view = [[SubViewOutOfSelfAlsoCanBeFirstresponseView alloc] initWithFrame:CGRectMake(0, 0, btnSize.width, btnSize.height)];
    [view addSubview:customButton];
    view.canBeResponseButton = customButton;
    
    // set the barbuttonitem to be the view
    return [[UIBarButtonItem alloc] initWithCustomView:view];
}

#pragma mark- 自定义的barItem样式
+ (UIBarButtonItem *)backBarItemWithTarget:(nullable id)target action:(SEL)action {
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.exclusiveTouch = YES;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//    [button setImage:[UIImage imageNamed:@"back_button"]forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"back_button"] forState:UIControlStateNormal];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 44, 45);
    return [UIBarButtonItem _barItemWithCustomButton:button];
}

+ (UIBarButtonItem *)editBarItemWithTarget:(nullable id)target action:(SEL)action  {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     button.exclusiveTouch = YES;
    [button setImage:[UIImage imageNamed:@"editBtn"]forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 44, 45);
    return [UIBarButtonItem _barItemWithCustomButton:button];
}

+ (UIBarButtonItem *)barItemWithImageName:(NSString *)imageName Target:(nullable id)target action:(SEL)action {
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0,0,44,45)];
    [button setImage:[UIImage imageNamed:imageName]forState:UIControlStateNormal];
     button.frame = CGRectMake(0, 0, 44, 45);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [UIBarButtonItem _barItemWithCustomButton:button];;
}

+ (UIBarButtonItem *)barItemWithTitle:(NSString *)aTitle Target:(nullable id)target action:(SEL)action {
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0,0,100,30)];
    [button setBackgroundImage:[UIImage imageNamed:@"Clear-All-240-70"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 100, 30);//(kDYBarHeight-45)/2.0
     [button setTitle:aTitle forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [UIBarButtonItem _barItemWithCustomButton:button];
}

@end
